package com.yunyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunyi.common.core.domain.AjaxResult;
import com.yunyi.common.utils.SecurityUtils;
import com.yunyi.system.domain.EmrCase;
import com.yunyi.system.domain.EmrCaseSlide;
import com.yunyi.system.mapper.EmrCaseSlideMapper;
import com.yunyi.system.service.EmrCaseService;
import com.yunyi.system.mapper.EmrCaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
* @author MoBing
* @description 针对表【emr_case(会诊病例)】的数据库操作Service实现
* @createDate 2023-09-14 09:25:07
*/
@Service
public class EmrCaseServiceImpl extends ServiceImpl<EmrCaseMapper, EmrCase>
    implements EmrCaseService{
    @Resource
    EmrCaseMapper emrCaseMapper;
    @Resource
    EmrCaseSlideMapper caseSlideMapper;

    /**
     * 新建病例
     * @param emrCase
     * @return
     */
    @Override
    @Transactional
    public AjaxResult saveByEmrCase(EmrCase emrCase) {
        //随机id
        Random random = new Random();
        int randomNumber = random.nextInt(9000000) + 1000000;
        String randomString = String.valueOf(randomNumber);
        // 新增预约
        EmrCaseSlide caseSlide = new EmrCaseSlide();
        caseSlide.setId(randomString);
        caseSlide.setCaseId(emrCase.getCase_no());
        caseSlide.setCreateBy(SecurityUtils.getLoginUser().getUser().getMobile());
        caseSlide.setCreateDate(new Date());
        caseSlide.setUpdateBy(SecurityUtils.getLoginUser().getUser().getMobile());
        caseSlide.setUpdateDate(new Date());
        caseSlide.setApmstatus(0);
        caseSlide.setApmdate(new Date());
        caseSlide.setSpecimen(emrCase.getSpecimen());
        caseSlide.setAccountId(SecurityUtils.getLoginUser().getUser().getAccount_id());
        caseSlide.setRemarks(emrCase.getRemarks());
        System.out.println(caseSlide.getId());
        boolean save = save(emrCase);
        int insert = caseSlideMapper.insert(caseSlide);
        if (insert>0){
            System.out.println("预约添加成功");
        }else{
            System.out.println("预约添加失败");
        }
        if(save){
            return AjaxResult.success("添加成功");
        }else{
            return AjaxResult.error("添加失败");
        }

    }
    /**
     * 根据id删除病例
     * @param id
     * @return
     */
    @Override
    public AjaxResult deleteById(String id) {
        int i = emrCaseMapper.deleteById(id);
        return i>0?AjaxResult.success("删除成功"):AjaxResult.error("删除失败");
    }

    @Override
    public List<EmrCase> getEmrCaseMsgListEmrCases(String caseIdOrName) {
        return emrCaseMapper.getEmrCaseMsgListEmrCases(caseIdOrName);
    }

    /**
     * 获取病例表和切片表的待分配及待撤回数量
     * wfd  病例待分配数量
     * tbw  病例待撤回数量
     * fzwfd    冰冻待分配数量
     * fztbw    冰冻待撤回数量
     *
     * @return
     */
    @Override
    public AjaxResult getTbt() {
        return AjaxResult.success(emrCaseMapper.getTbt());
    }

    /**
     * 获取近7日操作过的病例信息
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public AjaxResult getLastOperation(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<EmrCase> lastOperation = emrCaseMapper.getLastOperation();
        PageInfo<EmrCase> lastOperationInfo = new PageInfo<>(lastOperation);
        return lastOperation == null ? AjaxResult.error("近期暂无操作") : AjaxResult.success(lastOperationInfo);
    }

    /**
     * 根据条件查询待分配的病例信息
     *
     * @param pathology    条件1：病例类型 1.常规 2.细胞 3.冰冻 4.会诊
     * @param sendHospital 条件2：申请医院
     * @param caseNo       条件3：病理号
     * @param name         条件4：病人姓名
     * @param pageNum      默认：第1页
     * @param pageSize     默认：显示20条
     * @return
     */
    @Override
    public AjaxResult getTbd(Integer status, Integer pathology, String sendHospital, String caseNo, String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<EmrCase> caseList = emrCaseMapper.getTbd(status, pathology, sendHospital, caseNo, name);
        PageInfo<EmrCase> emrCasePageInfo = new PageInfo<>(caseList);
        return caseList == null ? AjaxResult.error("未查询到此数据") : AjaxResult.success("获取待分配病例信息", emrCasePageInfo);
    }

    /**
     * 批量分配病例
     *
     * @param caseList 病例id
     * @return
     */
    @Override
    public AjaxResult allotCase(List<String> caseList) {
        if (caseList == null || caseList.isEmpty()) {
            return AjaxResult.error("请选择要分配的病例");
        }
        //受影响行数
        int rows = emrCaseMapper.allotCase(caseList);
        return rows == caseList.size() ? AjaxResult.success("分配成功") : AjaxResult.error("部分数据分配失败！选中行数：" + caseList.size() + "，分配成功数目：" + rows);
    }

    @Override
    public AjaxResult getDiagnosticReport(String id) {
        EmrCase diagnosticReport = emrCaseMapper.getDiagnosticReport(id);
        return diagnosticReport == null ? AjaxResult.error("获取诊断报告单失败") : AjaxResult.success(diagnosticReport);
    }

    @Override
    public AjaxResult revocation(String caseId,String reason) {
        UpdateWrapper wrapper = new UpdateWrapper<>()
                .set("status", 4)
                .set("reason",reason)
                .set("update_date",new Date())
                .eq("id", caseId);
        int update = emrCaseMapper.update(null, wrapper);
        return update > 0 ? AjaxResult.success("撤回成功！") : AjaxResult.error("撤回失败");
    }

    /**
     * 条件查询诊断信息
     * @param emrCase2
     * @return
     */
    @Override
    public List<EmrCase> selectEmrCaseList(EmrCase emrCase2) {
        return emrCaseMapper.selectEmrCaseList(emrCase2);
    }


    /**
     * 根据会诊病例ID查询具体就诊人
     * @param id
     * @return
     */
    @Override
    public EmrCase selectEmrCaseById(Long id) {
        return emrCaseMapper.selectEmrCaseById(id);
    }
}




